// src/router/guards.js
import { useUserStore } from '@/stores/user';

export const setupRouterGuards = (router) => {
  router.beforeEach((to, from, next) => {
    const userStore = useUserStore();
    
    // 初始化用户信息
    if (!userStore.userInfo) {
      userStore.initUser();
    }
    
    // 检查是否需要认证
    if (to.meta.requiresAuth && !userStore.isLoggedIn) {
      next('/login');
      return;
    }
    
    // 检查角色权限
    if (to.meta.requiredRole) {
      const userRole = userStore.currentRole;
      if (userRole !== to.meta.requiredRole) {
        alert('您没有权限访问此页面');
        next(from.path);
        return;
      }
    }
    
    next();
  });
};

// 在main.js中调用
// import { setupRouterGuards } from '@/router/guards';
// setupRouterGuards(router);